package history

class HistoryService {

    def createHistory(def object, def session) {
        def account = Account.get(session.accountId)
        if (!account) {
            return
        }
        def history = new History(account: account, type: 'Create').save()
        if (!history) {
            return
        }
        object.history = history
        object.save()
        println "CreateHistory:"+object.class.name+",id:"+object.id+",name:"+object.toString()+",by:"+account.login
    }

    def updateHistory(def object, def session) {
        def account = Account.get(session.accountId)
        if (!account) {
            return
        }
        def history = new History(account: account, type: 'Update').save()
        if (!history) {
            return
        }
        object.history = history
        object.save()
        println "UpdateHistory:"+object.class.name+",id:"+object.id+",name:"+object.toString()+",by:"+account.login
    }

    def deleteHistory(def object, def session) {
        def account = Account.get(session.accountId)
        if (!account) {
            return
        }
        def history = new History(account: account, type: 'Delete').save()
        if (!history) {
            return
        }
        object.history = history
        object.save()
        println "DeleteHistory:"+object.class.name+",id:"+object.id+",name:"+object.toString()+",by:"+account.login
    }
}
